package com.qianyu.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.qianyu.project.core.annotation.FlowBegin;
import com.qianyu.project.core.annotation.FlowEntry;
import com.qianyu.project.entity.MaterialUseDetail;
import com.qianyu.project.entity.MaterialUsePlan;
import com.qianyu.project.mapper.MaterialUsePlanMapper;
import com.qianyu.project.service.IMaterialUseDetailService;
import com.qianyu.project.service.IMaterialUsePlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 材料需用计划 服务实现类
 * </p>
 *
 * @author gs
 * @since 2021-01-30
 */
@Service
public class MaterialUsePlanServiceImpl extends ServiceImpl<MaterialUsePlanMapper, MaterialUsePlan> implements IMaterialUsePlanService {

    @Autowired
    private MaterialUsePlanMapper mapper;
    @Autowired
    private IMaterialUseDetailService detailService;
    /**
     * 添加
     * description:  <br/>
     * author: lyz   <br/>
     * date: 2021/2/5 <br/>
     * params:  <br/>
     * return:
     */
    @FlowBegin("material_use_plan")
    public Integer saveMaterialUsePlan(@FlowEntry("id") MaterialUsePlan materialUsePlan
            ,List<MaterialUseDetail> detailList) {
        mapper.insert(materialUsePlan);
        detailList.forEach(detail->{
            detail.setUseId(materialUsePlan.getId());
        });
        detailService.saveBatch(detailList);
        return 1;
    }

    @Override
    public List getUsePlanList(Map<String, String> map) {
        QueryWrapper queryWrapper=new QueryWrapper<>();
        if(StringUtils.isNotBlank(map.get("planSubject"))){
            queryWrapper.like("plan_subject",map.get("planSubject"));
        }
        if(StringUtils.isNotBlank(map.get("applyer"))){
            queryWrapper.like("applyer",map.get("applyer"));
        }
        if(StringUtils.isNotBlank(map.get("sdate"))){
            queryWrapper.ge("date",map.get("sdate"));
        }
        if(StringUtils.isNotBlank(map.get("edate"))){
            queryWrapper.le("date",map.get("edate"));
        }
        if(StringUtils.isNotBlank(map.get("approveStatus"))){
            queryWrapper.eq("approve_status",map.get("approveStatus"));
        }
        return list(queryWrapper);
    }

    @Override
    public MaterialUsePlan getPlanById(String id) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("id",id);
        return getOne(queryWrapper);
    }
}
